import argparse

parser = argparse.ArgumentParser(description='the script used to generate rank table')
parser.add_argument('--device', type=str,default='0,1,2,3,4,5,6,7', help='the id of the devices to use')

args_opt, unknown = parser.parse_known_args()


devices = args_opt.device
devices = devices.split(',')
device_num = len(devices)
new_table={
    "board_id": "0x0020",
    "chip_info": "910",
    "deploy_mode": "lab",
    "group_count": "1",
    "group_list":[],
    "para_plane_nic_location": "device",
    "para_plane_nic_name":[],
    "para_plane_nic_num": str(device_num),
    "status": "completed"
}
group_list={
    "device_num": str(device_num),
    "server_num": "1",
    "group_name": "",
    "instance_count": str(device_num),
    "instance_list": []
}

devices_list={
    "0":{"devices": [{"device_id": "0","device_ip": "192.168.100.101"}],"rank_id": "0","server_id": "59.36.11.53"},
    "1":{"devices": [{"device_id": "1","device_ip": "192.168.101.101"}],"rank_id": "1","server_id": "59.36.11.53"},
    "2":{"devices": [{"device_id": "2","device_ip": "192.168.102.101"}],"rank_id": "2","server_id": "59.36.11.53"},
    "3":{"devices": [{"device_id": "3","device_ip": "192.168.103.101"}],"rank_id": "3","server_id": "59.36.11.53"},
    "4":{"devices": [{"device_id": "4","device_ip": "192.168.100.102"}],"rank_id": "4","server_id": "59.36.11.53"},
    "5":{"devices": [{"device_id": "5","device_ip": "192.168.101.102"}],"rank_id": "5","server_id": "59.36.11.53"},
    "6":{"devices": [{"device_id": "6","device_ip": "192.168.102.102"}],"rank_id": "6","server_id": "59.36.11.53"},
    "7":{"devices": [{"device_id": "7","device_ip": "192.168.103.102"}],"rank_id": "7","server_id": "59.36.11.53"}
}
para_plane_nic_name=[]
for rank,device in enumerate(devices):
    entry = devices_list[device]
    entry["rank_id"]=str(rank)
    group_list["instance_list"].append(entry)
    para_plane_nic_name.append("eth"+device)

new_table["group_list"].append(group_list)
new_table["para_plane_nic_name"]=para_plane_nic_name

import json
new_table=json.dumps(new_table,indent=4)

with open("rank_table_"+str(device_num)+"pcs.json","w") as f:
    f.writelines(new_table)
